package com.cn.goodsadmin.service;

import com.cn.goodsadmin.po.ActionRout;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

@Service("actionroutService")
public class ActionRoutService extends BaseService{

    public TreeMap<String,Integer> getActionRoutByDay(int day,int appid) throws Exception{
        List<ActionRout> actionRouts=actionRoutDao.getActionRoutByDay(day,appid);
        if(actionRouts!=null){
            final SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            TreeMap<String,Integer> map=new TreeMap<String, Integer>(new Comparator<String>() {

                public int compare(String o1, String o2){
                    try {
                        Date d1 = simpleDateFormat.parse(o1);
                        Date d2=simpleDateFormat.parse(o2);
                        if(d1.after(d2)){
                            return -1;
                        }else if(d1.before(d2)){
                            return 1;
                        }else{
                            return 0;
                        }
                    }catch (Exception e){

                    }
                    return 0;
                }
            });

            for(ActionRout actionRout:actionRouts){
                Date date=actionRout.getcTime();
                String timestring=simpleDateFormat.format(date);
                if(map.containsKey(timestring)){
                    Integer value=map.get(timestring);
                    map.put(timestring,value.intValue()+1);
                }else{
                    map.put(timestring,1);
                }
            }
            return map;
        }
        return null;
    }
}
